package cn.xiongxf.service.impl;



import cn.xiongxf.mapper.MovieMapper;
import cn.xiongxf.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import cn.xiongxf.domain.Movie;
import cn.xiongxf.domain.UserMovie;
import cn.xiongxf.service.MovieService;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Service
public class MovieServiceImpl implements MovieService {
    @Autowired
    private MovieMapper movieMapper;

    @Override
    public List<Movie> findAll() {
        return movieMapper.loadAll();
    }

    @Transactional
    @Override
    public AjaxResult purchase(UserMovie userMovie) {
        Movie movie = movieMapper.loadByMovieId(userMovie.getMovieId());
        if (movie != null){
            Integer stock = movie.getStock();
            if (stock < 1){
                return new AjaxResult(false,"当前电影票已售磬！");
            }else {
                // 余票-1
                movie.setStock(stock - 1);
                // 设置id
                movie.setId(userMovie.getMovieId());
                // 更新电影票余票信息
                movieMapper.update(movie);

                UserMovie userMovieDB = movieMapper.loadByUserIdAndMovieId(userMovie);
                if(userMovieDB == null){
                    // 如果不是相同用户买相同的票，添加中间表信息
                    movieMapper.saveUserMovie(userMovie);
                }
                return AjaxResult.me().setSuccess(true).setMessage("购票成功！");
            }
        }
        return new AjaxResult(false,"该电影不存在！");
    }

    @Override
    public BigDecimal queryPrice(Long id) {
        Movie movie = movieMapper.loadByMovieId(id);
        return movie.getPrice();
    }

    @Transactional
    @Override
    public void presented(Long id) {
        Movie movie = movieMapper.loadByMovieId(1L);
        Integer stock = movie.getStock();
        // 余票-1
        movie.setStock(stock - 1);
        // 设置id
        movie.setId(1L);
        // 更新电影票余票信息
        movieMapper.update(movie);

        // 保存中间表信息
        UserMovie userMovie = new UserMovie();
        userMovie.setMovieId(1L);
        userMovie.setUserId(id);
        movieMapper.saveUserMovie(userMovie);
    }

    @Override
    public AjaxResult watch(UserMovie userMovie) {
        UserMovie userMovieDB = movieMapper.loadByUserIdAndMovieId(userMovie);
        Movie movieDB = movieMapper.loadByMovieId(userMovie.getMovieId());
        if (userMovieDB != null){
            Date endTime = movieDB.getEnd_time();
            long end = endTime.getTime();
            Date date = new Date();
            long now = date.getTime();
            if (now - end < 0){
//                return new AjaxResult(true,"您所查询的电影票为" + movieDB.getName());
                return AjaxResult.me().setSuccess(true).setMessage("查询成功！").setResultObj(movieDB);
            }else {
                return AjaxResult.me().setSuccess(false).setMessage("该电影票已过期！").setResultObj(movieDB);
            }
        }else {
            return AjaxResult.me().setSuccess(false).setMessage("您未购买该电影票！").setResultObj(movieDB);
        }
    }


}
